package light.mvc.service.hrs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.hrs.Thrsmember;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.base.SessionInfo;
import light.mvc.pageModel.hrs.HRSMember;
import light.mvc.service.hrs.HRSMemberServiceI;
import light.mvc.utils.MD5Util;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HRSMemberServiceImpl implements HRSMemberServiceI{

	@Autowired
	private BaseDaoI<Thrsmember> memberDao;

	@Override
	public void add(HRSMember m) {
		Thrsmember t = new Thrsmember();
		t.setStatus(0);
		t.setLoginName(m.getLoginName());
		t.setRealName(m.getRealName());
		t.setPassword(MD5Util.md5(m.getPassword()));
		memberDao.save(t);
	}

	@Override
	public void delete(Long id) {
		Thrsmember t = memberDao.get(Thrsmember.class, id);
		memberDao.delete(t);
	}

	@Override
	public void edit(HRSMember m) {
		Thrsmember t = memberDao.get(Thrsmember.class, m.getId());
		t.setStatus(m.getStatus());
		t.setLoginName(m.getLoginName());
		t.setRealName(m.getRealName());
		t.setPassword(MD5Util.md5(m.getPassword()));
		memberDao.update(t);
	}

	@Override
	public HRSMember get(Long id) {
		Thrsmember t = memberDao.get(Thrsmember.class, id);
		HRSMember m = new HRSMember();
		m.setLoginName(t.getLoginName());
		m.setId(t.getId());
		m.setRealName(t.getRealName());
		m.setStatus(t.getStatus());
		return m;
	}

	@Override
	public List<HRSMember> dataGrid(HRSMember member, PageFilter ph) {
		List<HRSMember> ul = new ArrayList<HRSMember>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Thrsmember t ";
		List<Thrsmember> l = memberDao.find(hql + whereHql(member, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
		
		for (Thrsmember t : l) {
			HRSMember u = new HRSMember();
			BeanUtils.copyProperties(t, u);
			ul.add(u);
		}

		return ul;
	}

	@Override
	public Long count(HRSMember member, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Thrsmember t ";
		return memberDao.count("select count(*) " + hql + whereHql(member, params), params);
	}

	private String whereHql(HRSMember member, Map<String, Object> params) {
		String hql = "";
		if (member != null) {
			hql += " where 1=1 ";
//			搜索loginName或realName时用模糊查询
			if (member.getLoginName() != null) {
				hql += " and t.loginName like :loginName";
				params.put("loginName", "%%" + member.getLoginName() + "%%");
			}
			if (member.getRealName() != null) {
				hql += " and t.realName like :realName";
				params.put("realName", "%%" + member.getRealName() + "%%");
			}
			hql += " and t.status = :status";
			params.put("status", member.getStatus());
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	@Override
	public HRSMember getByLoginName(HRSMember member) {
		Thrsmember t = memberDao.get("from Thrsmember t  where t.loginName = '"+member.getLoginName()+"'");
		HRSMember u = new HRSMember();
		if(t!=null){
			BeanUtils.copyProperties(t, u);
		}else{
			return null;
		}
		return u;
	}

	@Override
	public void pass(Long id) {
		Thrsmember t = memberDao.get(Thrsmember.class, id);
		t.setStatus(1);
		memberDao.update(t);
	}
	

	@Override
	public HRSMember login(HRSMember member) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("loginName", member.getLoginName());
		params.put("password", MD5Util.md5(member.getPassword()));
		Thrsmember t = memberDao.get("from Thrsmember t where t.loginName = :loginName and t.password = :password and t.status = 1", params);
		if (t != null) {
			HRSMember u = new HRSMember();
//			对象拷贝
			BeanUtils.copyProperties(t, u);
			return u;
		}
		return null;
	}

	@Override
	public boolean editUserPwd(SessionInfo sessionInfo, String oldPwd,
			String pwd) {
		Thrsmember u = memberDao.get(Thrsmember.class, sessionInfo.getId());
		if (u.getPassword().equalsIgnoreCase(MD5Util.md5(oldPwd))) {// 说明原密码输入正确
			u.setPassword(MD5Util.md5(pwd));
			return true;
		}
		return false;
	}

}
